//
//  WbTabBar.swift
//  Weibo
//
//  Created by HH on 16/11/30.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

class WbTabBar: UITabBar {
    
     var callBack:(()->())?
    
      private lazy var composeButton:UIButton = {
       
        let button = UIButton()
        
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        
        button.addTarget(self, action: #selector(composeBtnAction(sender:)), for: .touchUpInside)
        
        return button
        
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc private func composeBtnAction(sender:UIButton)  {
        
        callBack?()
        
    }
    
    private func setupUI(){
        
        backgroundImage = #imageLiteral(resourceName: "tabbar_background")
        
        addSubview(composeButton)
        
        composeButton.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
       
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        let width = self.width / 5
        
        var index = 0
        
        
        for view in subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.width = width
                
                if index == 2 {
                    index += 1
                }
                
                view.x = CGFloat(index) * width
                
                index += 1
            }
            
        }
    }

}
